package top.huntwolf.admin.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.annotation.URLControl;
import top.huntwolf.admin.annotation.URLMessage;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.pojo.orderEntity.Order;
import top.huntwolf.admin.pojo.wxPay.*;
import top.huntwolf.admin.service.WxPayService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@URLControl
@Api(description = "微信支付控制器")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/wxPay")
public class WxPayController {

    @Autowired
    private WxPayService wxPayService;

    @URLMessage(url = "/wxPay")
    @PostMapping("")
    @ApiOperation("生成平台订单")
    public JsonResult<Map<String, String>> generatePlatformOrder(@RequestBody Order order){

        return JsonResult.success( wxPayService.wxPayPlaceOrder(order));

    }

    @PostMapping("/callback")
    @ApiOperation("获取微信支付通知")
    public WxPayNotifyResult payNotify(HttpServletRequest httpServletRequest) throws IOException {

        try {
            wxPayService.processPayNotice(httpServletRequest);
            return new WxPayNotifyResult("SUCCESS",null);
        } catch (Exception e) {
            return new WxPayNotifyResult("FAIL",e.getMessage());
        }

    }

    @URLMessage(url = "/wxPay/close/order-id/*")
    @GetMapping("/close/order-id/{orderId}")
    @ApiOperation("关闭订单接口")
    public JsonResult closePayByOrderId(@PathVariable String orderId){
        wxPayService.processPayClose(orderId);
        return JsonResult.success(CodeMsg.SUCCESS);
    }


    @PostMapping("/refund/callback")
    @ApiOperation("获取微信退款通知")
    public WxPayNotifyResult refundNotify(HttpServletRequest httpServletRequest) throws IOException {

        try {
            wxPayService.processRefundNotice(httpServletRequest);
            return new WxPayNotifyResult("SUCCESS",null);
        } catch (Exception e) {
            return new WxPayNotifyResult("FAIL",e.getMessage());
        }
    }

}


